import { createRouter, createWebHistory } from 'vue-router'

// 动态导入组件
const Layout = () => import('@/layout/index.vue')

// 自动导入views目录下的所有vue文件
const modules = import.meta.glob('../views/**/index.vue')

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: []
  }
]

// 动态生成路由
Object.keys(modules).forEach(path => {
  // 从路径中提取路由名称
  const match = path.match(/\.\.\/views\/(.+)\/index\.vue$/)
  if (match) {
    const routePath = match[1]
    const routeName = routePath.split('/').pop()
    
    routes[0].children.push({
      path: routePath,
      name: routeName,
      component: modules[path],
      meta: {
        title: routeName
      }
    })
  }
})

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router